package top.deepdesigner.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import top.deepdesigner.pojo.DtsStorage;
import top.deepdesigner.service.DtsStorageService;
import top.deepdesigner.util.AliyunOSSUtil;

import java.util.Date;
import java.util.List;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 11:00:08 <br/>
 */
@Service
public class DtsStorageServiceImpl extends ServiceImpl<BaseMapper<DtsStorage>, DtsStorage> implements DtsStorageService {

  //OSS服务器访问域名
  @Value("${aliyun.oss.endpoint}")
  private String endpoint;

  //子账户名
  @Value("${aliyun.oss.accessKeyId}")
  private String accessKeyId;

  //子账户密码
  @Value("${aliyun.oss.accessKeySecret}")
  private String accessKeySecret;

  //桶名字
  @Value("${aliyun.oss.bucketName}")
  private String bucketName;

  @Override
  @Transactional
  public DtsStorage uploadImageFile(MultipartFile file) throws Exception {
    //文件字节数组
    byte[] bytes = file.getBytes();
    String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename());
    long time = new Date().getTime();
    //文件名
    String fileName = time + extension;
    //上传后的url
    String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, fileName, bytes);
    DtsStorage dtsStorage = new DtsStorage();
    dtsStorage.setKey(fileName);
    dtsStorage.setName(fileName);
    dtsStorage.setType(extension);
    dtsStorage.setSize(file.getSize());
    dtsStorage.setUrl(url);
    dtsStorage.setAddTime(new Date());
    dtsStorage.setUpdateTime(new Date());
    dtsStorage.setDeleted(false);
    //保存到数据库
    this.save(dtsStorage);
    return dtsStorage;
  }

  @Override
  public Long getStorageCount() {
    return this.count(new LambdaQueryWrapper<DtsStorage>().eq(DtsStorage::getDeleted, false));
  }

  @Override
  public List<DtsStorage> getStorageList(Integer page, Integer limit, String sort, String order, String key, String name) {
    QueryWrapper<DtsStorage> wrapper = new QueryWrapper<>();
    wrapper.eq(true, "deleted", false)
        .like(!StringUtils.isEmpty(key), "`key`", key)
        .like(!StringUtils.isEmpty(name), "name", name)
        .orderBy(!StringUtils.isEmpty(sort), "asc".equalsIgnoreCase(order), sort);
    return this.page(new Page<>(page, limit), wrapper).getRecords();
  }

  @Override
  public DtsStorage updateStorage(DtsStorage os) {
    os.setUpdateTime(new Date());
    LambdaQueryWrapper<DtsStorage> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(DtsStorage::getId, os.getId());
    this.update(os, wrapper);
    return os;
  }

  @Override
  public void deleteStorage(DtsStorage os) {
    os.setDeleted(true);
    LambdaQueryWrapper<DtsStorage> wrapper = new LambdaQueryWrapper<>();
    wrapper.eq(DtsStorage::getId, os.getId());
    this.update(os, wrapper);
  }
}
